package QC.MessageBox.beans.appmsg;

import QC.MessageBox.exceptions.ParamException;
import io.vertx.core.json.JsonObject;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * Created by Andaren on 2017/4/13.
 * Used for: app端发送过来的消息
 */
public class BaseAppMsg {

    private String developerId;     // receiverId
    private String openId;          // channelUserId
    private String type;            // msgType
    private String content;         // msgContent
    private String channelType;     // channelType

    private String extra;           // 附加的消息

    private Integer plateformType;  // 发送者平台类型

    public BaseAppMsg() {}

    public BaseAppMsg(JsonObject json) {
        this.content = json.getString("content");
        this.developerId = json.getString("developerId");
        this.openId = json.getString("openId");
        this.type = json.getString("type");
        this.channelType = json.getString("channelType");
        this.extra = json.getString("extra");
    }

    /**
     * 从“&”连接的字符串中解析消息体
     * @param msgStr
     */
    public BaseAppMsg(String msgStr) throws ParamException, UnsupportedEncodingException {
        Objects.nonNull(msgStr);
        String[] msgArray = msgStr.split("&");
        if (msgArray != null && msgArray.length > 0) {
            Map<String, String> param = new HashMap<>();
            for (String s : msgArray) {
                String[] sa = s.split("=");
                if (sa != null && sa.length == 2) {
                    param.put(sa[0], sa[1]);
                } else {
                    throw new ParamException(s);
                }
            }
            // 解码
            this.developerId = URLDecoder.decode(param.get("developerId"), "utf-8");
            this.openId = URLDecoder.decode(param.get("openId"), "utf-8");
            this.type = URLDecoder.decode(param.get("type"), "utf-8");
            this.content = URLDecoder.decode(param.get("content"), "utf-8");
            if (param.get("channelType") != null) {
                this.channelType = URLDecoder.decode(param.get("channelType"), "utf-8");
            }
        } else {
            throw new ParamException("参数内容为空");
        }
    }

    public String getDeveloperId() {
        return developerId;
    }

    public BaseAppMsg setDeveloperId(String developerId) {
        this.developerId = developerId;
        return this;
    }

    public String getOpenId() {
        return openId;
    }

    public BaseAppMsg setOpenId(String openId) {
        this.openId = openId;
        return this;
    }

    public String getContent() {
        return content;
    }

    public BaseAppMsg setContent(String content) {
        this.content = content;
        return this;
    }

    public String getType() {
        return type;
    }

    public BaseAppMsg setType(String type) {
        this.type = type;
        return this;
    }

    public String getChannelType() {
        return channelType;
    }

    public BaseAppMsg setChannelType(String channelType) {
        this.channelType = channelType;
        return this;
    }

    public String getExtra() {
        return extra;
    }

    public BaseAppMsg setExtra(String extra) {
        this.extra = extra;
        return this;
    }

    public Integer getPlateformType() {
        return plateformType;
    }

    public BaseAppMsg setPlateformType(Integer plateformType) {
        this.plateformType = plateformType;
        return this;
    }
}
